package top.wshape1.shiziapi.uaa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import top.wshape1.shiziapi.common.constant.BizCodeEnum;
import top.wshape1.shiziapi.uaa.domain.LoginUser;
import top.wshape1.shiziapi.uaa.domain.User;
import top.wshape1.shiziapi.uaa.mapper.UserMapper;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-08-22
 */

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    public UserMapper userMapper;

    /**
     * 获取用户通过邮箱
     *
     * @param email
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getEmail, email));

        if (user == null)
            throw new UsernameNotFoundException(BizCodeEnum.EMAIL_PASSWORD_ERROR.msg());

        // TODO 用户权限设置等

        return new LoginUser(user);
    }

}
